# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""Test Code_view module."""

import pytest
import pytest_check as check

from msticpy.common.utility import is_ipython
from msticpy.vis import code_view

__author__ = "Ian Hellen"

# pylint: disable=redefined-outer-name

_INPUT_TEXT = r"""
$GDN_VERSION_CLI_FOUND = $false
$PARSE_GDN_VERSION_CLI = $false
$GDN_ARGUMENTS = @()

foreach ($argument in $Arguments)
{
    if ($argument -ieq "--guardian-version")
    {
        $GDN_VERSION_CLI_FOUND = $PARSE_GDN_VERSION_CLI = $true
    }
    elseif ($GDN_VERSION_CLI_FOUND)
    {
        $GDN_VERSION = $argument
        $PARSE_GDN_VERSION_CLI = $false
    }
    else
    {
        $GDN_ARGUMENTS += $argument
    }
}

if (-not [String]::IsNullOrWhiteSpace($Policy))
{
    $GDN_ARGUMENTS += "--policy"
    $GDN_ARGUMENTS += $Policy
}
"""

_CORE_HTML = """
<div class="highlight"><pre><span></span><span class="nv">$GDN_VERSION_CLI_FOUND
</span> <span class="p">=</span> <span class="nv">$false</span>
"""

_HTML_STYLE_HDR = """
<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=None">
  <style type="text/css">
/*
generated by Pygments <https://pygments.org/>
"""


def _core_html():
    return _CORE_HTML.replace("\n", "")


_CODE_VIEW_TESTS = [
    ("powershell", None, None, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", None, False, (_core_html(), None)),
    ("powershell", None, True, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", "default", None, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", "stata-dark", True, (_core_html(), _HTML_STYLE_HDR)),
]


@pytest.mark.parametrize("lang, style, full, expected", _CODE_VIEW_TESTS)
def test_to_html(lang, style, full, expected):
    """Test to_html function."""
    core_html, style_hdr = expected
    kwargs = {"language": lang, "style": style, "full": full}
    if style is None:
        del kwargs["style"]
    if full is None:
        del kwargs["full"]

    html = code_view.to_html(_INPUT_TEXT, **kwargs)
    print(html)
    check.is_in(core_html, html)
    if full:
        check.is_in(style_hdr, html)


def test_list_pygments_styles():
    """Test expected styles in pygments styles."""
    styles = code_view.list_pygments_styles()
    check.is_in("stata-dark", styles)
    check.is_in("default", styles)


@pytest.mark.parametrize("lang, style, full, expected", _CODE_VIEW_TESTS)
def test_display_code(lang, style, full, expected):
    """Simple test for display_code."""
    del expected
    kwargs = {"language": lang, "style": style, "full": full}
    if style is None:
        del kwargs["style"]
    if full is None:
        del kwargs["full"]

    handle = code_view.display_html(_INPUT_TEXT, **kwargs)
    if is_ipython(notebook=True):
        check.is_not_none(handle)
